﻿// <copyright file="Scope.cs" company="Beuth Hochschule für Technik">
// Breakdown - Configure your Windows 7 Firewall
// Copyright (C) 2009 Stefan "dersteps@gmail.com" Matyba
// This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 
// either version 3 of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/.
// </copyright>
// <author>Stefan Matyba</author>
// <email>dersteps@gmail.com</email>
// <date>2009-10-18</date>
// <summary>Breakdown Application Logic - Implementation of a scoped firewall rule decorator</summary>

namespace Breakdown.Logic
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Net;
    using System.Text;
    using Breakdown.Aspects;

    /// <summary>
    /// Defines a scoped rule decoration
    /// </summary>
    [Logged]
    public class Scope : IScoped
    {
        /// <summary>
        /// List of local IP addresses affected by a rule
        /// </summary>
        private Collection<IPAddress> _localAddresses;

        /// <summary>
        /// List of remote IP addresses affected by a rule
        /// </summary>
        private Collection<IPAddress> _remoteAddresses;

        /// <summary>
        /// If true, the rule affects any local IP address
        /// </summary>
        private bool _anyLocal;

        /// <summary>
        /// If true, the rule affects any remote IP address
        /// </summary>
        private bool _anyRemote;

        /// <summary>
        /// Initializes a new instance of the Scope class. By default, all remote and local IP addresses are affected
        /// </summary>
        public Scope()
        {
            this._localAddresses = new Collection<IPAddress>();
            this._remoteAddresses = new Collection<IPAddress>();
            this._anyRemote = true;
            this._anyLocal = true;
        }

        /// <summary>
        /// Gets the list of local IP addresses affected
        /// </summary>
        public Collection<IPAddress> LocalAddresses
        {
            get { return this._localAddresses; }
        }

        /// <summary>
        /// Gets the list of remote IP addresses affected
        /// </summary>
        public Collection<IPAddress> RemoteAddresses
        {
            get { return this._remoteAddresses; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not all local IP addresses are affected
        /// </summary>
        public bool AnyLocalAddress
        {
            get { return this._anyLocal; }
            set { this._anyLocal = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not all remote IP addresses are affected
        /// </summary>
        public bool AnyRemoteAddress
        {
            get { return this._anyRemote; }
            set { this._anyRemote = value; }
        }

        /// <summary>
        /// Adds a local IP address to the scope
        /// </summary>
        /// <param name="ip">IP address to add</param>
        /// <returns>Amount of local IP addresses in the scope after addition</returns>
        public int AddLocal(IPAddress ip)
        {
            this._localAddresses.Add(ip);
            return this._localAddresses.Count<IPAddress>();
        }

        /// <summary>
        /// Removes a local IP address from the scope
        /// </summary>
        /// <param name="ip">IP address to remove</param>
        /// <returns>Amount of local IP addresses after removal</returns>
        public int RemoveLocal(IPAddress ip)
        {
            this._localAddresses.Remove(ip);
            return this._localAddresses.Count<IPAddress>();
        }

        /// <summary>
        /// Removes all local IP addresses from the scope
        /// </summary>
        public void ClearLocal()
        {
            this._localAddresses.Clear();
        }

        /// <summary>
        /// Adds a remote IP address to the scope
        /// </summary>
        /// <param name="ip">IP address to add</param>
        /// <returns>Amount of remote IP addresses in the scope after addition</returns>
        public int AddRemote(IPAddress ip)
        {
            this._remoteAddresses.Add(ip);
            return this._remoteAddresses.Count<IPAddress>();
        }

        /// <summary>
        /// Removes a remote IP address from the scope
        /// </summary>
        /// <param name="ip">IP address to remove</param>
        /// <returns>Amount of remote IP addresses after removal</returns>
        public int RemoveRemote(IPAddress ip)
        {
            this._remoteAddresses.Remove(ip);
            return this._remoteAddresses.Count<IPAddress>();
        }

        /// <summary>
        /// Removes all remote IP addresses from the scope
        /// </summary>
        public void ClearRemote()
        {
            this._remoteAddresses.Clear();
        }
    }
}
